#!/usr/bin/env bash
#build llvm on unix system

ClangbuilderDir=$(cd `dirname $0`; pwd)
if [[ ! -f "$ClangbuilderDir/branch" ]]; then
  Branch="release_50"
  Release="RELEASE_500/final"
else
  source "$ClangbuilderDir/branch"
fi

function Restore-Repositories(){
    pushd $PWD
    if [[ ! -d "$1" ]]; then
      echo "Please check $1 is exists"
      return
    fi
    if [ -d "$1/$2/.svn" ]; then
        cd "$1/$2"
        svn cleanup .
        svn up .
    else
        cd "$1"
        svn co $3 $2
    fi
    echo "ClangBuilder checkout $2 done."
    popd
}

function Update-LLVM(){
  sourcedir=$1
  suburl=$2
  Restore-Repositories "$ClangbuilderDir" "$sourcedir" "http://llvm.org/svn/llvm-project/llvm/$suburl"
  Restore-Repositories "$ClangbuilderDir/$sourcedir/tools" clang "http://llvm.org/svn/llvm-project/cfe/$suburl"
  Restore-Repositories "$ClangbuilderDir/$sourcedir/tools" lld "http://llvm.org/svn/llvm-project/lld/$suburl"
  Restore-Repositories "$ClangbuilderDir/$sourcedir/tools" lldb "http://llvm.org/svn/llvm-project/lldb/$suburl"
  Restore-Repositories "$ClangbuilderDir/$sourcedir/tools/clang/tools" extra "http://llvm.org/svn/llvm-project/clang-tools-extra/$suburl"
  Restore-Repositories "$ClangbuilderDir/$sourcedir/projects" compiler-rt "http://llvm.org/svn/llvm-project/compiler-rt/$suburl"
  Restore-Repositories "$ClangbuilderDir/$sourcedir/projects" libcxx "http://llvm.org/svn/llvm-project/libcxx/$suburl"
  Restore-Repositories "$ClangbuilderDir/$sourcedir/projects" libcxxabi "http://llvm.org/svn/llvm-project/libcxxabi/$suburl"
  Restore-Repositories "$ClangbuilderDir/$sourcedir/projects" openmp "http://llvm.org/svn/llvm-project/openmp/$suburl"
}


CC=clang
CXX=clang++

hash clang 2>/dev/null || {
  echo "We need use gcc to build llvm and clang."
  CC=gcc
  CXX=g++
}

function Gen-ProjectFile(){
  srcdir=$1
  cmake -DCMAKE_BUILD_TYPE=Release  -DLLVM_ENABLE_ASSERTIONS=OFF -DCMAKE_C_COMPILER="$CC" \
  -DCMAKE_CXX_COMPILER="$CXX" -DCLANG_REPOSITORY_STRING="clangbuilder.io" "$srcdir"
  if [[ -f "Makefile" ]]; then
    #statements
    echo "Begin to compile LLVM"
    make
  fi
}


function Build-Mainline(){
  srcdir="mainline"
  suburl="trunk"
  Update-LLVM $srcdir $suburl
  if [[ ! -d "$ClangbuilderDir/mainline_out" ]]; then
    mkdir "$ClangbuilderDir/mainline_out"
    #statements
  fi
    cd "$ClangbuilderDir/mainline_out"
  Gen-ProjectFile "$ClangbuilderDir/$srcdir"
}

function Build-Release(){
  srcdir="release"
  suburl="tags/$Release"
  Update-LLVM $srcdir $suburl
  if [[ ! -d "$ClangbuilderDir/release_out" ]]; then
    mkdir "$ClangbuilderDir/release_out"
    #statements
  fi
  cd "$ClangbuilderDir/release_out"
  Gen-ProjectFile "$ClangbuilderDir/$srcdir"
}

function Build-Stable(){
  srcdir="$Branch"
  suburl="branches/$Branch"
  Update-LLVM $srcdir $suburl
  if [[ ! -d "$ClangbuilderDir/stable_out" ]]; then
    mkdir "$ClangbuilderDir/stable_out"
    #statements
  fi
  cd "$ClangbuilderDir/stable_out"
  Gen-ProjectFile "$ClangbuilderDir/$srcdir"
}

function usage(){
cat<<EOF
Clangbuilder helper
usage: clangbuilder [options]
       stable          build llvm stable branch
       trunk           build llvm trunk revision
       release         build llvm last released revision
       help            Print clangbuilder help
EOF
exit 0
}

for arg ;do
	case "$arg" in
    help) usage ;;
    release) Build-Release ;;
    trunk) Build-Mainline;;
    stable) Build-Stable
esac
done
